﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.Configuration;
using System.Collections.Generic;

namespace Crochet.Configuration
{
	public class CrochetConfigurationCollectionElement<TConfigurationElementType> : ConfigurationElementCollection
		where TConfigurationElementType : CrochetConfigurationElement, new()
	{
		public override ConfigurationElementCollectionType CollectionType
		{
			get
			{
				return ConfigurationElementCollectionType.BasicMap;
			}
		}

		protected override string ElementName
		{
			get
			{
				return "add";
			}
		}

		protected override ConfigurationPropertyCollection Properties
		{
			get
			{
				return new ConfigurationPropertyCollection();
			}
		}

		protected override ConfigurationElement CreateNewElement()
		{
			return new TConfigurationElementType();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((TConfigurationElementType)element).Name;
		}

		public void Add(TConfigurationElementType element)
		{
			this.BaseAdd(element);
		}

		public void Remove(string element)
		{
			this.BaseRemove(element);
		}

		public void Clear()
		{
			this.BaseClear();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		/// <returns></returns>
		public TConfigurationElementType this[int index]
		{
			get
			{
				return (TConfigurationElementType)base.BaseGet(index);
			}
			set
			{
				if (base.BaseGet(index) != null)
				{
					base.BaseRemoveAt(index);
				}
				base.BaseAdd(index, value);
			}
		}

		public TConfigurationElementType this[object oKey]
		{
			get
			{
				return (TConfigurationElementType)base.BaseGet(oKey);
			}
		}

		public object[] GetAttributes(string oKey)
		{
			return ((TConfigurationElementType)base.BaseGet(oKey)).GetAttributes();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="item"></param>
		public void Remove(TConfigurationElementType item)
		{
			base.BaseRemove(item);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		public void RemoveAt(int index)
		{
			base.BaseRemoveAt(index);
		}
	}
}
